package com.hotau.product;

import java.util.ArrayList;

import org.codehaus.jackson.type.TypeReference;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.hotau.HotauActivity;
import com.hotau.HotauConstants;
import com.hotau.R;
import com.hotau.product.vo.Comment;
import com.hotau.product.vo.CommentStatics;
import com.hotau.product.vo.Product;
import com.hotau.util.DialogUtil;
import com.hotau.util.HttpRequestAsynTask;
import com.hotau.util.HttpUtil;
import com.hotau.util.JsonParserUtil;

public class CommentListFragment extends Fragment implements
		OnItemClickListener {
	private ArrayList<Comment> comments;
	private CommentAdapter adapter;
	private CommentStatics commentStatics;
	private Product product;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.product_comment_list, container, false);
	}

	private void doRequest() {
		String url = HotauConstants.LOCATION_QUERY_COMMENT;
		String request = "Form:productId=" + product.getProductId();

		HttpRequestAsynTask task = new HttpRequestAsynTask(getActivity()) {
			@SuppressWarnings("unchecked")
			@Override
			protected void doPostExecute(String result) {
				commentStatics = (CommentStatics) JsonParserUtil.str2Object(
						result, CommentStatics.class);

				if (commentStatics != null && commentStatics.errorMsg == null) {
					comments = (ArrayList<Comment>) JsonParserUtil
							.str2ObjectByTag(result, "allComment", 
									new TypeReference<ArrayList<Comment>>() {});
					commentStatics.comments = comments;
					adapter.notifyDataSetChanged();
				} else {
					if (commentStatics != null) {
						DialogUtil.showDialog(getActivity(), "提示", commentStatics.errorMsg);
					} else {
						DialogUtil.showDialog(getActivity(), "提示", "网络错误！");
					}
				}

			}
		};

		task.execute(url, request, HttpUtil.UTF8_ENCODING, HttpUtil.HTTP_POST);
	}

	@Override
	public void onActivityCreated(Bundle bundle) {
		super.onActivityCreated(bundle);
		setHasOptionsMenu(true);

		if (bundle != null) {
			product = (Product) bundle.getSerializable("product");
		}

		((Button) this.getActivity().findViewById(R.id.product_comment_btn))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Bundle bundle = new Bundle();
						bundle.putSerializable("product", product);
						AddCommentFragment fragment = new AddCommentFragment();
						fragment.setArguments(bundle);
						
						HotauActivity csmbpActivity = (HotauActivity) getActivity();
						csmbpActivity.pushFragment("我要点评", fragment);
					}
				});

		ListView listView = (ListView) this.getActivity().findViewById(
				R.id.product_comment_list);
		listView.setOnItemClickListener(this);

		if (commentStatics == null) {
			this.comments = new ArrayList<Comment>();
			doRequest();
		}

		adapter = new CommentAdapter();
		listView.setAdapter(adapter);
	}

	private void mockComment() {
		Comment comment = new Comment();
		comment.commentUserId = "哈啊哈";
		comment.commentDate = "2011-12-09 12:00";
		comment.comment1Count = "4";
		comment.commentContent = "很好好强大";
		comments.add(comment);
	}

	class CommentAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return comments == null ? 0 : comments.size();
		}

		@Override
		public Object getItem(int position) {
			return comments.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(getActivity()).inflate(
						R.layout.product_comment_item, null);
				holder = new ViewHolder();
				holder.userNameView = (TextView) convertView
						.findViewById(R.id.product_comment_item_username);
				holder.descriptionView = (TextView) convertView
						.findViewById(R.id.product_comment_item_description);
				holder.timeView = (TextView) convertView
						.findViewById(R.id.product_comment_item_time);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			Comment comment = (Comment) this.getItem(position);
			holder.userNameView.setText(comment.commentUserId);
			holder.timeView.setText(comment.commentDate);
			holder.descriptionView.setText(comment.commentContext);
			return convertView;
		}

		class ViewHolder {
			// ImageView iconView;
			TextView userNameView;
			TextView descriptionView;
			TextView timeView;
			// RatingBar ratingBar;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		HotauActivity activity = (HotauActivity) this.getActivity();

		Comment comment = comments.get(arg2);
		Bundle bundle = new Bundle();
		bundle.putSerializable("comment", comment);
		Fragment fragment = new CommentDetailFragment();
		fragment.setArguments(bundle);
		activity.pushFragment("点评内容", fragment);
	}
}
